
// <copyright file="GenericDataReader.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The generic data reader.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System;

    using SubhadraSolutions.Sharp.Utils.Data.Common;

    /// <summary>
    /// The generic data reader.
    /// </summary>
    public sealed class GenericDataReader : AbstractDataReader
    {
        #region Fields

        /// <summary>
        /// The strategy.
        /// </summary>
        private IDataReaderStrategy _strategy;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericDataReader"/> class.
        /// </summary>
        /// <param name="strategy">
        /// The strategy.
        /// </param>
        public GenericDataReader(IDataReaderStrategy strategy)
        {
            this.Strategy = strategy;
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets or sets the strategy.
        /// </summary>
        public IDataReaderStrategy Strategy
        {
            get
            {
                return this._strategy;
            }

            set
            {
                this._strategy = value;
                this.ColumnNames.Clear();
                this.ColumnNames.AddRange(this._strategy.ColumnNames);
            }
        }

        #endregion Public Properties

        #region Public Indexers

        /// <summary>
        /// The this.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public override object this[int i]
        {
            get
            {
                return this._strategy[i];
            }
        }

        #endregion Public Indexers

        #region Public Methods and Operators

        /// <summary>
        /// The get field type.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="Type"/>.
        /// </returns>
        public override Type GetFieldType(int i)
        {
            Type fieldType = this._strategy.GetFieldType(i);
            return fieldType ?? base.GetFieldType(i);
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// The dispose.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            this._strategy.Dispose();
        }

        /// <summary>
        /// The read protected.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        protected override bool readProtected()
        {
            return this._strategy.Read();
        }

        #endregion Methods
    }
}